<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Promotions Extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->page_auth->secure_page(['registrar','president']);
		$this->view_data['system_message'] = $this->_msg();
	}
	
	public function index()
	{
		$this->view_data['hash'] = $this->hs->encrypt(time());
	}
	
	public function elementary($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x AND $this->_check_time($x[0]))
		{
			
			//load our new PHPExcel library
			$this->load->library('excel');
			
			//all grade level in elementary
			$grade_level = $this->_get_grade_level_elementary('elementary');
			
			
			// pd($this->db->last_query());
			//loop through grade level to create multiple sheet for all grade levels
			foreach($grade_level as $k => $v)
			{	
				ini_set('memory_limit','500M');
				ini_set('max_execution_time',60);
				
				//students
				$names_male = $this->_get_names_and_addresses_by_level($v->level_id,'male');
				$names_female = $this->_get_names_and_addresses_by_level($v->level_id,'female');
				//get subjects
				$subjects = $this->_get_level_subjects();
				
				//create new sheet
				$objectSheet = $this->excel->createSheet($k); 
				//set the sheet title
				$objectSheet->setTitle($v->level_desc);
				//heading part of promotional reports set values
				$objectSheet->setCellValue('A4','CURRICULUM RBEC');
				$objectSheet->setCellValue('A10','NAMES');
				$objectSheet->setCellValue('C10','HOME ADDRESS');
				$objectSheet->setCellValue('C1','DEPARTMENT OF EDUCATION');
				$objectSheet->setCellValue('C2','BUREAU OF SECONDARY SCHOOLS');
				$objectSheet->setCellValue('C3','DIVISION OF CITY SCHOOLS -ANGELES');
				$objectSheet->setCellValue('C6','REPORT ON SECONDARY PROMOTION');
				$objectSheet->setCellValue('D10','YEARS in SCHOOL');
				$objectSheet->setCellValue('E10','AGE');
				$objectSheet->setCellValue('E8',strtoupper($this->school_name));
				$objectSheet->setCellValue('F10','Total Number of Days Attendance in Curriculum Year');
				$objectSheet->setCellValue('G11','Final Rating');
				$objectSheet->setCellValue('H11','Action Taken');
				$objectSheet->setCellValue('I11','Final Rating');
				$objectSheet->setCellValue('J11','Action Taken');
				$objectSheet->setCellValue('K11','Final Rating');
				$objectSheet->setCellValue('L11','Action Taken');
				$objectSheet->setCellValue('M11','Final Rating');
				$objectSheet->setCellValue('N11','Action Taken');
				$objectSheet->setCellValue('O11','Final Rating');
				$objectSheet->setCellValue('P11','Action Taken');
				$objectSheet->setCellValue('Q11','Final Rating');
				$objectSheet->setCellValue('R11','Action Taken');
				$objectSheet->setCellValue('S11','Final Rating');
				$objectSheet->setCellValue('T11','Action Taken');
				$objectSheet->setCellValue('U11','Final Rating');
				$objectSheet->setCellValue('V11','Action Taken');
				$objectSheet->setCellValue('W11','Final Rating');
				$objectSheet->setCellValue('X11','Action Taken');
				$objectSheet->setCellValue('Y11','Final Rating');
				$objectSheet->setCellValue('Z11','Action Taken');
				$objectSheet->setCellValue('AA11','Final Rating');
				$objectSheet->setCellValue('AB11','Action Taken');
				$objectSheet->setCellValue('M8','MUNICIPALITY ');
				$objectSheet->setCellValue('W1','Due in the division office one week before the last.');
				$objectSheet->setCellValue('W2','Prescribed Regular Class Day for Third year students.');
				$objectSheet->setCellValue('W3','Due in the division office on the Monday following the last');
				$objectSheet->setCellValue('W4','prescribed  for other secondary students.');
				$objectSheet->setCellValue('S6','CURRICULUM YEAR SECOND');
				$objectSheet->setCellValue('Y6','SECTION SUBSISTO');
				
				//mergeCells for promotial headers
				$objectSheet->getStyle('C1')->getFont()->setSize(11);
				$objectSheet->getStyle('C2')->getFont()->setSize(11);
				$objectSheet->getStyle('C3')->getFont()->setSize(11);
				$objectSheet->getStyle('C6')->getFont()->setSize(16);
				$objectSheet->getStyle('G11:AB11')->getFont()->setSize(7);
				$objectSheet->getStyle('A10:F10')->getFont()->setSize(7);
				$objectSheet->mergeCells('C1:V1');
				$objectSheet->mergeCells('C2:V2');
				$objectSheet->mergeCells('C3:V3');
				$objectSheet->mergeCells('C6:R6');
				$objectSheet->mergeCells('E8:K8');
				$objectSheet->mergeCells('M8:R8');
				$objectSheet->mergeCells('S6:V6');
				$objectSheet->mergeCells('W1:AP1');
				$objectSheet->mergeCells('W2:AE2');
				$objectSheet->mergeCells('W3:AP3');
				$objectSheet->mergeCells('W4:AF4');
				$objectSheet->mergeCells('Y6:AA6');
				$objectSheet->mergeCells('A4:C4');
				
				$objectSheet->mergeCells('A10:B10');
				$objectSheet->mergeCells('A10:A11');
				$objectSheet->mergeCells('C10:C11');
				$objectSheet->mergeCells('D10:D11');
				$objectSheet->mergeCells('E10:E11');
				$objectSheet->mergeCells('F10:F11');
				
				$objectSheet->mergeCells('G10:H10');
				$objectSheet->mergeCells('I10:J10');
				$objectSheet->mergeCells('K10:L10');
				$objectSheet->mergeCells('M10:N10');
				$objectSheet->mergeCells('O10:P10');
				$objectSheet->mergeCells('Q10:R10');
				$objectSheet->mergeCells('S10:T10');
				$objectSheet->mergeCells('U10:V10');
				$objectSheet->mergeCells('W10:X10');
				$objectSheet->mergeCells('Y10:Z10');
				$objectSheet->mergeCells('AA10:AB10');
				
				$objectSheet->getStyle('A10:F10')->getAlignment()->setWrapText(true); 
				$objectSheet->getStyle('G11:AB11')->getAlignment()->setWrapText(true); 
				
				// $objectSheet->getStyle("A2:B2")->applyFromArray(array("font" => array( "bold" => true)));
				$objectSheet->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
				$objectSheet->getStyle('C2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
				$objectSheet->getStyle('C3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
				$objectSheet->getStyle('C6')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
				$objectSheet->getStyle('C8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
				$objectSheet->getStyle('A10')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
				$objectSheet->getStyle('C10')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
			
			
			
				//set the subjects
				
				$objectSheet->setCellValue('G10',$subjects[0]);
				$objectSheet->setCellValue('I10',$subjects[1]);
				$objectSheet->setCellValue('K10',$subjects[2]);
				$objectSheet->setCellValue('M10',$subjects[3]);
				$objectSheet->setCellValue('O10',$subjects[4]);
				$objectSheet->setCellValue('Q10',$subjects[5]);
				$objectSheet->setCellValue('S10',$subjects[6]);
				$objectSheet->setCellValue('U10',$subjects[7]);
				$objectSheet->setCellValue('W10',$subjects[8]);
				$objectSheet->setCellValue('Y10',$subjects[9]);
				$objectSheet->setCellValue('AA10',$subjects[10]);
				
			
			
				//setting the names/address and other values here
				// pd($names_male);
				$objectSheet->setCellValue('B12','MALES');
				$male_count = 13;
				foreach($names_male as $k => $v)
				{
					$objectSheet->setCellValue('A'.$male_count,$k+1);
					$objectSheet->setCellValue('B'.$male_count,ucwords(strtolower($v->fullname)));
					$objectSheet->setCellValue('C'.$male_count,$v->address);
					$objectSheet->setCellValue('E'.$male_count,$v->age);
					$male_count++;
					
				}
				
				$female_count = $male_count;
				$objectSheet->setCellValue('B'.$female_count,'FEMALE');
				foreach($names_female as $k => $v)
				{
					$fc = $female_count +1;
					$objectSheet->setCellValue('A'.$fc,$k+1);
					$objectSheet->setCellValue('B'.$fc,ucwords(strtolower($v->fullname)));
					$objectSheet->setCellValue('C'.$fc,$v->address);
					$objectSheet->setCellValue('E'.$fc,$v->age);
					$female_count++;
				}
			}
			
			@ob_end_clean();
			
			$filename='elementary_reports'.date('m_d_y_h-i-s',time()).'.xls'; //save our workbook as this file name
			header('Content-Type: application/vnd.ms-excel'); //mime type
			header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
			header('Cache-Control: max-age=0'); //no cache
			//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
			//if you want to save it as .XLSX Excel 2007 format				
			$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
			 @ob_end_clean();
			//force user to download the Excel file without writing it to server's HD
			$objWriter->save('php://output');
			@$objectSheet->disconnectWorksheets();
			unset($objectSheet);
		
		}else{
			show_404();
		}
	}
	
	public function highschool($hash = '')
	{
		$x = $this->hs->decrypt($hash);
		if($x AND $this->_check_time($x[0]))
		{
		
		
		
		}else{
			show_404();
		}
	}
	
	private function _get_letter($num)
	{
		$numeric = $num % 26;
		$letter = chr(65 + $numeric);
		$num2 = intval($num / 26);
		if ($num2 > 0) {
			return $this->_get_letter($num2 - 1) . $letter;
		} else {
			return $letter;
		}
	}
	
	private function _get_names_and_addresses_by_level($level,$gender)
	{
		$this->load->model('M_student');
		$data = $this->M_student->get_students_for_promotional_reports($level,$gender);
		return $data;
	}
	
	private function _get_level_subjects($id = '')
	{
		return ['subject1','subject2','subject3','subject4','subject5','subject6','subject7','subject8','subject9','subject10','subject11'];
	}
	private function _get_student_grades(){}
	
	private function _get_grade_level_elementary($grade_level)
	{
		$this->load->model('M_levels');
		return $this->M_levels->get_levels_for_promotions($grade_level);
	}
}

